<?php


namespace app\index\controller;

use app\admin\service\RbacService;
use app\common\controller\CommonController;
use app\common\exception\ParameterFailedException;
use app\common\exception\NotFoundException;
use app\common\model\AddressModel;
use app\common\model\XXXXXModel;
use app\common\service\Lang;
use app\common\validate\XXXXXValidate;
use think\facade\Db;

class XXXXXController extends CommonController
{
    public function getXXXXXList()
    {

        $models      = (new XXXXXModel())->getXXXXXList($request->userModel->getData('id'));
        return retJson(Lang::GETDone, $models->toArray());
    }

    public function postXXXXX()
    {

        $params      = self::getParam([
            'nickname',
        ]);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $oldModel = (new XXXXXModel())->where('id_card', $params['id_card'])->find();
            if ($oldModel) {
                throw new ParameterFailedException(['msg' => '已存在该数据']);
            }

            $model           = new XXXXXModel();
            $model->nickname = $params['nickname'];

            $model->save();

            Db::commit();
            return retJson(Lang::POSTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function putXXXXX()
    {

        $params      = self::getParam([
            'id',
            'xxxxx',
        ]);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }

            $carModel = YYYYYModel::where('driver_id', $params['id'])->find();
            if ($carModel) {
                throw new NotFoundException(['msg' => '该xx已被yy绑定，请解除绑定后继续操作']);
            }

            $model->nickname = $params['nickname'];
            $model->save();

            Db::commit();
            return retJson(Lang::PUTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function deleteXXXXX()
    {

        $params      = self::getParam(['id']);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }


            $otherModel = OtherModel::where('id', $params['id'])->find();
            if (!$otherModel) {
                throw new NotFoundException();
            }

            $model->delete_time = NOW_TIME;
            $model->save();

            Db::commit();

            return retJson(Lang::DELETEDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }
}